//
//  ScrollView.m
//  婚礼记
//
//  Created by Lovetong on 15/2/8.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ScrollView.h"
#import "DetailScrollModel.h"
#import "CustomImageView.h"
#import "UIImageView+WebCache.h"

@interface ScrollView ()<UIScrollViewDelegate>

@property (retain, nonatomic) UIScrollView *scrollView;
@property (retain, nonatomic) UIPageControl *pageControl;
@property (assign, nonatomic) NSInteger page;

@end

@implementation ScrollView

- (instancetype)initWithFrame:(CGRect)frame timerAnimationTime:(CGFloat)time pageControl:(BOOL)pageControl
{
    self = [self initWithFrame:frame];
    if (self) {
        if (pageControl) {
            _page = 1;
            self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 20)];
            [_pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
            [_pageControl setCurrentPage:1];
            [self addSubview:_pageControl];
            [_pageControl release];
        }
        [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray array];
        self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
        [self.scrollView setPagingEnabled:YES];
        [self.scrollView setDelegate:self];
        [self addSubview:_scrollView];
        [_scrollView release];
    }
    return self;
}

- (void)setScrollViewWithImageArray:(NSMutableArray *)imageInfoDicArray imageUrlKey:(NSString *)imageUrlKey
{
    CGSize offSize = self.frame.size;
    CGSize size = CGSizeZero;
    [_pageControl setNumberOfPages:[imageInfoDicArray count]];
    [imageInfoDicArray insertObject:[imageInfoDicArray lastObject] atIndex:0];
    [imageInfoDicArray addObject:[imageInfoDicArray objectAtIndex:1]];
    [self.scrollView setContentSize:CGSizeMake(self.frame.size.width * [imageInfoDicArray count], 0)];
    [_scrollView setBackgroundColor:[UIColor whiteColor]];
    for (int i = 0; i < [imageInfoDicArray count]; i++) {
        NSURL *imageUrl = [NSURL URLWithString:[[imageInfoDicArray objectAtIndex:i] valueForKey:imageUrlKey]];
        CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(i * offSize.width, 0, offSize.width, offSize.height)];
        [imageView setImageInfo:[imageInfoDicArray objectAtIndex:i]];
        [imageView setImageWithURL:imageUrl];
        if ([imageView.imageInfo isKindOfClass:[DetailScrollModel class]] && ![[imageView.imageInfo valueForKey:@"width"] isKindOfClass:[NSNull class]] && ![[imageView.imageInfo valueForKey:@"height"] isKindOfClass:[NSNull class]]) {
            size.width = [[imageView.imageInfo valueForKey:@"width"] floatValue];
            size.height = [[imageView.imageInfo valueForKey:@"height"] floatValue];
            UIView *view = [self setImageView:imageView Size:size toSize:offSize];
            [view setFrame:CGRectMake(i * offSize.width, 0, offSize.width, offSize.height)];
            [_scrollView addSubview:view];
        } else {
            [_scrollView addSubview:imageView];
        }
        [imageView release];
    }
    [self.scrollView setContentOffset:CGPointMake(offSize.width, 0)];
}

- (UIView *)setImageView:(CustomImageView *)imageView Size:(CGSize)currentSize toSize:(CGSize)size
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    [view setClipsToBounds:YES];
    [imageView setFrame:CGRectMake(0, 0, size.height * currentSize.width / currentSize.height, size.height)];
    [imageView setCenter:view.center];
    [view addSubview:imageView];
    return [view autorelease];
}

- (void)updateScrollView:(CGFloat)contentOffsetHeight
{
    CGSize offSize = self.frame.size;
    CGPoint offSet = self.frame.origin;
    CGSize size = CGSizeZero;
    static CGFloat curentContentOffset = 0;
    [self setFrame:CGRectMake(offSet.x, offSet.y, offSize.width, offSize.height - contentOffsetHeight + curentContentOffset)];
    for (int i = 0; i < [[self.scrollView subviews] count]; i++) {
        UIView *view = [[self.scrollView subviews] objectAtIndex:i];
        [_scrollView setFrame:CGRectMake(_scrollView.frame.origin.x, contentOffsetHeight, _scrollView.frame.size.width, offSize.height)];
        [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, offSize.width, offSize.height)];
        CustomImageView *imageView = (CustomImageView *)[[view subviews] firstObject];
        size.width = imageView.frame.size.width;
        size.height = imageView.frame.size.height;
        [imageView setFrame:CGRectMake((offSize.width - offSize.height / size.height *size.width) / 2.0f, 0, offSize.height / size.height *size.width, offSize.height)];
    }
    curentContentOffset = contentOffsetHeight;
}

- (void)timerAction:(NSTimer *)timer
{
    _pageControl.currentPage = _page;
    self.page = [_scrollView contentOffset].x / _scrollView.frame.size.width;
    _page++;
    [UIView animateWithDuration:0.5 animations:^{
        [_scrollView setContentOffset:CGPointMake(_page * _scrollView.frame.size.width, 0)];
    }];
    if (_page == _scrollView.contentSize.width / self.frame.size.width - 1) {
        _page = 1;
        [_pageControl setCurrentPage:0];
        [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_scrollView.contentOffset.x == [_scrollView contentSize].width - self.frame.size.width) {
        [_scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
        [_pageControl setCurrentPage:1];
        _page = _pageControl.currentPage;
    } if (_scrollView.contentOffset.x == 0) {
        [_scrollView setContentOffset:CGPointMake([_scrollView contentSize].width - self.frame.size.width * 2, 0)];
        [_pageControl setCurrentPage:[_scrollView contentOffset].x / self.frame.size.width - 1];
        _page = _pageControl.currentPage;
    }
    [_pageControl setCurrentPage:[_scrollView contentOffset].x / self.frame.size.width - 1];
    _page = _pageControl.currentPage;
}

- (void)dealloc
{
    [_array release];
    [_pageControl release];
    [_scrollView release];
    [super dealloc];
}

@end
